package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.repository.ICategoryRepository;
import cn.tedu.tmall.admin.mall.pojo.param.CategoryParam;
import cn.tedu.tmall.admin.mall.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.mall.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CategoryService implements ICategoryService {
    @Autowired
    private ICategoryRepository categoryRepository;

    @Override
    public void addCategory(CategoryParam categoryName) {
        log.debug("添加类别参数：入参{}", categoryName);
        String name = categoryName.getName();
        log.debug("添加类别：入参{}", name);
        Integer count = categoryRepository.countCategoryByName(name);
        log.debug("添加类别：入参{},出参:{}",name, count);
        if (count == 0){
            log.debug("类别为空,类别名称:{}",name);
            CategoryPO newcategoryPO = new CategoryPO();
            BeanUtils.copyProperties(categoryName,newcategoryPO);
            log.debug("保存类别名称：入参{}",newcategoryPO);
            categoryRepository.save(newcategoryPO);
            log.debug("保存类别：入参{}，成功",newcategoryPO);
        }else {
            log.debug("类别不能为空，类别名称:{}",name);
            throw new RuntimeException("类别名称存在");
        }

    }
}
